package com.pinyin4j;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 
 * <获取汉字拼音工具类>
 * <PinyinHelper.toHanyuPinyinStringArray方法支持同一汉字有多个发音，比如,“单”对应的拼音有：dan1、chan2、shan4；
 *	还支持拼音的格式化输出，比如第几声之类的，拼音后面的数字就是代表第几声；其返回的数据类型是一个String数组，
 *  它用来接收一个汉字的多个发音如果 toHanyuPinyinStringArray中的参数不是汉字，那么它会返回null>
 * @author  huanglz
 * @version  [版本号, 2012-1-16]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class PinYin4jUtil {

	/**
	 * 构造函数
	 */
	public PinYin4jUtil() {
	}

	/**
	 * <获取汉字全拼音>
	 * <功能详细描述>
	 * @param str
	 * @return [参数说明]
	 * 
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static String getPinYinFullChar(String str) {
		StringBuffer pybf = new StringBuffer();
		char t1[] = str.toCharArray();
		String t2[] = new String[t1.length];
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		try {
			for (int i = 0; i < t1.length; i++)
				if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i],
							defaultFormat);
					pybf.append(t2[0]);
				} else {
					pybf.append(t1[i]);
				}

		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		// 替换符号“-”
		return pybf.toString().replaceAll("\\W", "").trim();
	}

	/**
	 * <获取汉字首字母拼音>
     * <功能详细描述>
	 * @param str
	 * @return [参数说明]
	 * 
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static String getPinYinFirstChar(String str) {
		StringBuffer pybf = new StringBuffer();
		// String.length()中中英文都只算一个字符
		for (int j = 0; j < str.length(); j++) {
			char word = str.charAt(j);
			String pinyinArray[] = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null)
				// 只取一个发音，如果是多音字，仅取第一个发音，此处取第一个发音的首字母
				pybf.append(pinyinArray[0].charAt(0));
			else
				// 如果word不是汉字，toHanyuPinyinStringArray会返回null，保持原样
				pybf.append(word);
		}
		// 替换符号“-”
		return pybf.toString().replaceAll("\\W", "").trim();
	}
	
	public static void main(String[] args) {
		String nameString = "SS1-运维工程师";
		String pinyinName = getPinYinFirstChar(nameString);
		System.out.println(pinyinName);
	}
	
	/**
	  * 
	  *   CREATE TABLE `t_label_url` (
		  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '标签自增id',
		  `labelName` varchar(200) DEFAULT NULL COMMENT '标签名称',
		  `hasUrl` char(1) DEFAULT '0' COMMENT '是否有URL 1有0没有',
		  `url` varchar(200) DEFAULT NULL COMMENT '标签URL',
		  `create_date` datetime NOT NULL COMMENT '关系绑定时间',
		  `labelAlias` varchar(200) DEFAULT NULL COMMENT '标签全拼缩写',
		  PRIMARY KEY (`id`),
		  KEY `idx_t_label_url_alias` (`labelAlias`)
		) ENGINE=InnoDB AUTO_INCREMENT=133 DEFAULT CHARSET=utf8;
	  * 
	  CREATE TABLE `t_label_analysis` (
		  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '标签自增id',
		  `l_id` int(11) NOT NULL COMMENT '标签id',
		  `l_name` varchar(200) DEFAULT NULL COMMENT '标签分词',
		  `create_date` datetime NOT NULL COMMENT '关系绑定时间',
		  PRIMARY KEY (`id`),
		  KEY `idx_t_label_analysis_alias` (`l_name`)
		) ENGINE=InnoDB AUTO_INCREMENT=238 DEFAULT CHARSET=utf8;
		
			
	<!-- 根据ID查询标签信息 -->
   <SQL ID="list_label_by_ids"  ConnectionKey="aceona_old">
     	<CommandText>
	     <![CDATA[
	     select * from t_label_url where id in (?l_ids) ]]>
     </CommandText> 
     <ParameterCollection>	
		<Parameter Name="l_ids" Type="Other" DefaultValue="0"/>
	  </ParameterCollection>
   </SQL>
   <!-- 查询相近标签 -->
   <SQL ID="list_near_label"  ConnectionKey="aceona_old">
     	<CommandText>
	     <![CDATA[
	     select l_id ,COUNT(*) as l_count from t_label_analysis t where l_name in (
        select l_name from t_label_analysis where l_id = (select id from t_label_url where labelName = ?labelName and hasUrl = '0' )
        ) GROUP BY l_id HAVING l_id <>  (select id from t_label_url where labelName = ?labelName and hasUrl = '0' ) ORDER BY l_count DESC, create_date DESC  ;
     ]]>
     </CommandText> 
     <ParameterCollection>	
		<Parameter Name="labelName" Type="String" />
	  </ParameterCollection>
   </SQL>
   <!-- 查询相关标签 -->
   <SQL ID="list_label"  ConnectionKey="aceona_old">
     	<CommandText>
	     <![CDATA[
	       select id,labelName,hasUrl,url,create_date,labelAlias from t_label_url where labelAlias  like '?labelalias%'  and  hasUrl  = '0' 
        ]]>
     </CommandText> 
     <ParameterCollection>	
		<Parameter Name="labelalias" Type="Other" />
	  </ParameterCollection>
   </SQL>
   <!-- 不带条件查询标签列表 -->
   <SQL ID="list_label_all"  ConnectionKey="aceona_old">
     	<CommandText>
	     <![CDATA[
	       select id,labelName,hasUrl,url,create_date,labelAlias from t_label_url where hasUrl  = '1' 
        ]]>
     </CommandText> 
   </SQL>
   <!-- 首字母查询标签列表 -->
   <SQL ID="list_label"  ConnectionKey="aceona_old">
     	<CommandText>
	     <![CDATA[
	       select id,labelName,hasUrl,url,create_date,labelAlias from t_label_url where labelAlias  like '?labelalias%'  and  hasUrl  = '0' 
        ]]>
     </CommandText> 
     <ParameterCollection>	
		<Parameter Name="labelalias" Type="Other" />
	  </ParameterCollection>
   </SQL>
	<!-- 0-9数字查询标签列表 -->
   <SQL ID="list_label_num"  ConnectionKey="aceona_old">
     	<CommandText>
	     <![CDATA[
	     select  id,labelName,hasUrl,url,create_date,labelAlias  from t_label_url where labelAlias  REGEXP '^[0-9]'   and  hasUrl  = '0'
        ]]>
     </CommandText> 
   </SQL>
   
   	<!-- 0-9数字查询标签列表 -->
   <SQL ID="label_analysis_list"  ConnectionKey="aceona_old">
     	<CommandText>
	     <![CDATA[
	     select l_name from t_label_analysis where l_id = (select id from t_label_url where labelName = ?labelName)
        ]]>
     </CommandText> 
     <ParameterCollection>	
		<Parameter Name="labelName" Type="String" />
	  </ParameterCollection>
   </SQL>
			
	*/
}
